package com.zhou.bjgl.bjgl.business_web.controller;


import com.zhou.bjgl.bjgl.business_web.entity.BNotice;
import com.zhou.bjgl.bjgl.business_web.entity.BNoticeRead;
import com.zhou.bjgl.bjgl.business_web.service.BNoticeService;
import com.zhou.bjgl.bjgl.exception.BwException;
import com.zhou.bjgl.bjgl.exception.BwExceptionEnum;
import com.zhou.bjgl.bjgl.myAnnotations.UserLoginToken;
import com.zhou.bjgl.bjgl.req.BaseModel;
import com.zhou.bjgl.bjgl.req.b_notice.BNoticeAddReq;
import com.zhou.bjgl.bjgl.req.b_notice.BNoticeQueryReq;
import com.zhou.bjgl.bjgl.req.b_notice.BNoticeReadReq;
import com.zhou.bjgl.bjgl.resp.PageResp;
import com.zhou.bjgl.bjgl.resp.common.ApiRestResponse;
import com.zhou.bjgl.bjgl.resp.notice.BNoticeResp;
import com.zhou.bjgl.bjgl.resp.notice.NoticeNotReadResp;
import com.zhou.bjgl.bjgl.util.CopyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tools.ant.taskdefs.Copy;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  通知模块
 * </p>
 *
 * @author jz
 * @since 2021-07-04
 */
@RestController
@RequestMapping("/bjgl/b-notice")
@Api("通知管理")
public class BNoticeController extends BaseModel {

    @Resource
    private BNoticeService bNoticeService;

    @ApiOperation("发送通知")
    @PostMapping("/send")
    @UserLoginToken
    public ApiRestResponse sendNotice(@RequestBody BNoticeAddReq req) {
        if (threadLocal.get().getClassId() == null) {
            throw new BwException(BwExceptionEnum.NOT_DISTRIBUTION_CLASS);
        }
        req.setSendUser(threadLocal.get().getId());
        req.setToClass(threadLocal.get().getClassId());
        bNoticeService.AddNotice(req);
        return ApiRestResponse.success("通知发送成功");
    }


    @ApiOperation("查看通知列表")
    @GetMapping("/list")
    @UserLoginToken

    public ApiRestResponse getNoticeList(BNoticeQueryReq req) {
        req.setClassId(threadLocal.get().getClassId());
        PageResp<BNoticeResp> bNoticeRespList=bNoticeService.listNotice(req);
        return ApiRestResponse.success(bNoticeRespList);
    }

    @ApiOperation("获取通知详情")
    @GetMapping("/getDetail")
    public ApiRestResponse getNoticeDetail(BNoticeQueryReq req){
        BNoticeResp noticeById = bNoticeService.queryNoticeDetail(req);
        return ApiRestResponse.success(noticeById);
    }

    @ApiOperation("修改通知")
    @PostMapping("/editNotice")
    public ApiRestResponse editNotice(@RequestBody BNoticeAddReq req){


        bNoticeService.editNotice(req);
        return ApiRestResponse.success();
    }


    @ApiOperation("标记通知已读")
    @PostMapping("/noticeRead")
    @UserLoginToken
    public ApiRestResponse NoticeRead(@RequestBody BNoticeReadReq bNoticeReadReq) {

        BNoticeRead bNoticeRead = new BNoticeRead();
        bNoticeRead.setNoticeId(bNoticeReadReq.getNoticeId());
        bNoticeRead.setUserId(threadLocal.get().getId());
        bNoticeService.markNoticeRead(bNoticeRead);
        return ApiRestResponse.success();
    }

    @ApiOperation("获取未读通知列表")
    @GetMapping("listNotRead")
    @UserLoginToken
    public ApiRestResponse listNotRead(BNoticeReadReq bNoticeReadReq) {
        bNoticeReadReq.setUserId(threadLocal.get().getId());
        bNoticeReadReq.setClassId(threadLocal.get().getClassId());
        PageResp<NoticeNotReadResp> notReadRespPageResp =bNoticeService.queryNoticeNotRead(bNoticeReadReq);
        return ApiRestResponse.success(notReadRespPageResp);
    }

    @ApiOperation("删除通知")
    @PostMapping("deleteNotice")
    @UserLoginToken
    public ApiRestResponse deleteNotice(@RequestBody BNoticeReadReq bNoticeReadReq){
        bNoticeService.removeById(bNoticeReadReq.getId());
        return ApiRestResponse.success();
    }
}

